<?php

namespace App\Http\Controllers\Admin;

use App\Model\Feedback;
use App\Model\UserModel;
use App\Model\NoticeModel;
use App\Model\AdminLogsModel;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class NoticeController extends Controller
{
    //
    public function index(Request $request)
    {
        $news = NoticeModel::where("type", 1)->orderByRaw('sort desc,created_at desc')->where(function ($query) {
            if (request()->title) {
                $query->where('title', 'like', "%" . request()->title . "%");
            }
        })->paginate(15);
        return view('admin.notice.index', compact('request', 'news'));
    }

    // 修改排序
    public function update_sort(Request $request)
    {
        $region = NoticeModel::find($request->id);
        $region->sort = $request->sort;
        if ($region->save()) {
            return ['code' => 1, 'msg' => '修改成功'];
        }
        return ['code' => 0, 'msg' => '修改失败'];
    }

    //修改公告状态
    public function change_state()
    {
    	$id = request()->id;
    	$state = ['1'=>'开启','2'=>'关闭'];
        $na = NoticeModel::where('id', $id)->value('title');
        if (NoticeModel::where('id', $id)->update(["state" => request()->value])){
			$msg = '更改公告【'.$na.'】(id:'.$id.') 状态为'.$state[request()->value];
			AdminLogsModel::addLog($msg);
            return redirect('/admin/notice/index')->with('success', '更改成功');
        } else {
            return back()->with('danger', '更改失败');
        }
    }

    //添加页面
    public function create()
    {
        return view('admin.notice.create');
    }

    //添加
    public function store()
    {
        if(!request()->title){
			return ['code'=>0,'msg'=>'请输入标题'];
		}
		if(!request()->content){
			return ['code'=>0,'msg'=>'请输入公告详情'];
		}
        if (NoticeModel::create([
            'title' => request()->title,
            'content' => request()->content,
            'state' => request()->state,
        ])) {
        	$msg = '添加公告【'.request()->title.'】';
        	AdminLogsModel::addLog($msg);
            return ['code'=>1,'msg'=>'添加成功'];
        } else {
            return ['code'=>1,'msg'=>'添加失败'];
        }
    }

    //修改页面
    public function edit()
    {
    	$id = request()->id;
        $new = NoticeModel::where('id', $id)->first();
        return view('admin.notice.edit', compact('new'));
    }

    //修改
    public function update()
    {
    	if(!request()->title){
    		return ['code'=>0,'msg'=>'请输入标签'];
		}
    	if(!request()->content){
    		return ['code'=>0,'msg'=>'请输入内容'];
		}
        $this->validate(request(), [
            'title' => 'required',
            'content' => 'required',
        ], [
            'title.required' => '请输入标签',
            'content.required' => '请输入内容',
        ]);
        if (NoticeModel::where("id", request()->id)->update([
            'title' => request()->title,
            'content' => request()->content,
        ])) {
			$msg = '编辑公告【'.request()->title.'】(id:'.request()->id.')';
			AdminLogsModel::addLog($msg);
            return ['code'=>1,'msg'=>'修改成功'];
        } else {
			return ['code'=>0,'msg'=>'修改失败'];
        }
    }

    //上传图品
	public function upload(){
		$img= upload('file','notice');
		if($img['code'] == 1){
			return '{"code":0,"msg":"成功上传","data":{"src":"'.$img['data'].'"}}';
		}else{
			$arr['code'] = 1;
			$arr['msg'] = '上传失败';
			return json($arr);
		}
	}

    //修改商品单属性
    public function del()
    {
    	$id = request()->id;
        $na = NoticeModel::where('id', $id)->value('title');
        if (NoticeModel::where('id', $id)->delete()) {
        	$msg = '刪除公告【'.$na.'】(id:'.$id.')';
        	AdminLogsModel::addLog($msg);
            return redirect('/admin/notice/index')->with('success', '删除成功');
        } else {
            return back()->with('danger', '删除失败');
        }
    }


    // 消息通知
    public function information(Request $request)
    {
        $news = NoticeModel::where("type", 2)->orderByRaw('sort desc,created_at desc')->where(function ($query) {
            if (request()->title) {
                $query->where('title', 'like', "%" . request()->title . "%");
            }

            if (request()->keywords) {
                $query->where('uid', request()->keywords);
            }
        })->paginate(15);
        return view('admin.information.index', compact('request', 'news'));
    }

    public function information_create()
    {
        return view('admin.information.create');
    }

    //添加
    public function information_store()
    {
		if(!request()->title){
			return ['code'=>0,'msg'=>'请输入标题'];
		}
		if(!request()->content){
			return ['code'=>0,'msg'=>'请输入详情'];
		}
		DB::beginTransaction();
		try{
			switch(request()->type){
				case '1':
					$user = UserModel::get();
					foreach($user as $k=>$v){
						if(!NoticeModel::where('type',2)->where('uid',$v['id'])->where('title',request()->title)->first()){
							NoticeModel::create([
								'uid' => $v->id,
								'title' => request()->title,
								'content' => request()->content,
								'type' => 2
							]);
						}
					}
					break;
				case '2':
					if(!userModel::where('id',request()->uid)->first()){
						return ['code'=>0,'msg'=>'该用户不存在'];
					}
					if(!request()->uid){
						return ['code'=>0,'msg'=>'请输入用户ID'];
					}
					NoticeModel::create([
						'uid' => request()->uid,
						'title' => request()->title,
						'content' => request()->content,
						'type' => 2
					]);
					break;
			}
			$msg = '添加消息通知内容【'.request()->title.'】';
			AdminLogsModel::addLog($msg);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'添加失败'];
		}

    }


    //修改页面
    public function detail($id)
    {
        $new = NoticeModel::where('id', $id)->first();
        return view('admin.information.detail', compact('new'));
    }

}
